import assert from 'assert'
import Methods from '../common/http-methods'
import ControllerServeHolder from '../controller/controller-serve-holder'
export default function serve(url?: string, methods?: Methods | Methods[]) {
    if (methods) {
        if (methods instanceof Array) {
            assert(
                methods.every((method) => method in Methods),
                `部分method有问题：[${methods}]`
            )
        } else {
            assert(methods in Methods, `method 有问题：[${methods}] `)
        }
    }
    function ser(target: Function): void
    function ser(target: object, field: string): void
    function ser(target: any, field?: string) {
        if (field) {
            // console.log('method serve:  ', target.constructor, field)
            ControllerServeHolder.serveMethod(target.constructor, field, url, methods)
        } else {
            assert(!methods, `在类上使用【serve】修饰时，禁止指定method`)
            // console.log('class serve:  ', target)
            ControllerServeHolder.serve(target, url)
        }
    }
    return ser
}
